package com.fang.mmy.user.service.lmpl;

import com.fang.mmy.user.service.RocketMQService;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName RocketMQImpl
 * @Author kebukeyi
 * @Date 2022/8/12 11:43
 * @Description
 * @Version 1.0.0
 */
@Service
public class RocketMQImpl implements RocketMQService {

    private static String LOGIN_TOPIC = "user_login";


    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void sendMessage(String id) {
        System.out.println("已进入处理队列，id:" + id);
        rocketMQTemplate.asyncSend(LOGIN_TOPIC, "userId:" + id, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消息已成功发送 : " + sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("消息发送异常，发送失败 : " + id);
                throwable.printStackTrace();
            }
        });
    }
}
